#include <WholeFormat.h>

WholeFormat::WholeFormat()
{
	addNewUnit( MILE.toBase(1) ,"MILE");
	addNewUnit( YARD.toBase(1) ,"YARD");
	addNewUnit( FEET.toBase(1) ,"FEET");
	addNewUnit( INCH.toBase(1) ,"INCH");
}

void WholeFormat::addNewUnit(const UINT& factorToBase,const string& unitName)
{
	UnitData unitData = {factorToBase, unitName};
	unitVector_m.push_back(unitData);
}

void WholeFormat::formatWithUnit(std::ostream& sio,const UnitData& unitData,UINT& valueRemain, UINT& valueInUnit)
{
	valueInUnit = valueRemain/unitData.factorToBase;
	
	if(0 != valueInUnit)
		sio<<valueInUnit<<" "<<unitData.name;
	
	valueRemain = valueRemain%unitData.factorToBase;
	return ;	
}

void WholeFormat::output(std::ostream& sio, const UINT& valueInBase)
{
	UINT valueRemain = valueInBase,valueInUnit = 0;
	for(vector<UnitData>::size_type index=0;index != unitVector_m.size();++index)
	{
		formatWithUnit(sio,unitVector_m[index],valueRemain,valueInUnit);
		if((index != unitVector_m.size()-1) && (0 != valueInUnit) && (0 != valueRemain))
			sio<<" ";
	}
	return;
}
